package com.sky.controller.user;

import com.alibaba.fastjson.JSON;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderSubmitVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * ClassName: OrderController
 * Package:
 * Description
 * 订单接口
 *
 * @Author xw
 * @Create 2023/11/25 18:08
 */

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "Pc 端-订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private WebSocketServer webSocketServer;

    @PostMapping("/submit")
    @ApiOperation("用户下单")
    public Result PostSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);

        OrderSubmitVO orderSubmitVO = orderService.PostSubmit(ordersSubmitDTO);

        return Result.success(orderSubmitVO);
    }


    /**
     * 订单支付（阉割版修改状态）
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<String> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);

        String id = orderService.payment(ordersPaymentDTO);

        Map map = new HashMap();
        map.put("type", 1);//消息类型，1表示来单提醒
        map.put("orderId", id); // 菜品 id
        map.put("content", "订单号：" + ordersPaymentDTO.getOrderNumber()); // 订单


        // 推送消息
        webSocketServer.sendToAllClient(JSON.toJSONString(map));


        return Result.success();
    }


    /**
     * 历史订单查询
     */
    @GetMapping("/historyOrders")
    @ApiOperation("历史订单查询")
    public Result<PageResult> historyOrders(int page, int pageSize, Integer status) {

        log.info("历史订单查询 page = {}，pageSize = {}，status = {}", page, pageSize, status);
        PageResult pageResult = orderService.historyOrders(page, pageSize, status);
        return Result.success(pageResult);
    }

    /**
     * 查询订单详情
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<Orders> orderDetailID(@PathVariable Integer id) {
        log.info("查询订单详情：{}", id);
        Orders orders = orderService.orderDetailID(id);

        return Result.success(orders);
    }


    /**
     * 取消订单
     */
    @PutMapping("/cancel/{id}")
    @ApiOperation("取消订单")
    public Result<String> cancelId(@PathVariable Long id) {

        log.info("取消订单：{}", id);

        orderService.cancelId(id);


        return Result.success();
    }


    /**
     * 再来一单
     */
    @PostMapping("/repetition/{id}")
    @ApiOperation("再来一单")
    public Result<String> repetitionId(@PathVariable Long id) {

        log.info("再来一单：{}", id);

        orderService.repetitionId(id);

        return Result.success();
    }


    /**
     * 催单
     */
    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable("id") Long id) {
        orderService.reminder(id);
        return Result.success();
    }
}
